package com.engine.zhlx.financemoduel.cashplan.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.zhlx.financemoduel.cashplan.utils.CashPlanUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-09-10
 * @Description: 资金计划同步SAP汇率信息
 * @ModifyLog:
 **/

public class WeaCashPlanWeb extends BaseBean {
    /**
     * @Description: SAP汇率信息同步
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/saveRateData")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String saveRateData(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        CashPlanUtil cashPlanUtil = new CashPlanUtil();
        String code = "SUCCESS";
        JSONObject data = new JSONObject();
        String message = "调用成功";
        try {
            writeLog(">>>>>saveRateData>>>paramsJson=" + paramsJson);
            int modelId = Util.getIntValue(paramsJson.getString("modelId"), -1);
            if (!cashPlanUtil.isModelId(modelId)) {
                code = "PARAMETER_ERROR";
                data.put("error", "modelId不存在");
                message = "请求参数错误";
            } else {
                String items = Util.null2String(paramsJson.getString("items"));
                if (cashPlanUtil.isJSONArray(items)) {
                    JSONArray dataArray = JSONArray.parseArray(items);
                    int counts = dataArray.size();
                    int count = 0;
                    for (int i = 0; i < dataArray.size(); i++) {
                        JSONObject dataJson = dataArray.getJSONObject(i);
                        int dataid = cashPlanUtil.insertModelData(modelId, dataJson);
                        writeLog(">>>>>saveRateData>>>dataid=" + dataid);
                        if (dataid > 0) {
                            count++;
                        }
                    }
                    data.put("sumcount", counts);
                    data.put("finishcount", count);
                    data.put("failcount", counts - count);
                } else {
                    data.put("error", "JSON数组格式化错误");
                    code = "PARAMETER_FORMAT_ERROR";
                    message = "参数格式转化异常";
                }

            }
        } catch (Exception e) {
            data.put("error", "程序内部异常");
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        apidatas.put("MSG_TYPE", "SUCCESS".equals(code)?"S":"E");
        apidatas.put("MSG_TEXT", message);
        writeLog(">>>>>doCreateRequest>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * @Description: 资金系统调用接口付款信息到一般用款申请/项目用款申请流程表单
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/postbackPaymentToWF")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String postbackPaymentToWF(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String code = "SUCCESS";
        JSONObject data = new JSONObject();
        String message = "操作成功";
        CashPlanUtil cashPlanUtil = new CashPlanUtil();
        try {
            writeLog(">>>>>postbackPaymentToWF>>>paramsJson=" + paramsJson);
            String formId = Util.null2String(paramsJson.getString("formId"));
            String requestId = Util.null2String(paramsJson.getString("requestId"));
            String formtype = Util.null2String(paramsJson.getString("formtype"));
            String mainid = cashPlanUtil.getMainidByReq(requestId, formId);
            if ("".equals(mainid)) {
                code = "PARAMETER_ERROR";
                data.put("error", "requestId或fomrId不存在");
                message = "请求参数错误";
            } else {
                RecordSet rs = new RecordSet();
                String tablename = "formtable_main_" + formId + "_" + formtype;
                String items = Util.null2String(paramsJson.getString("items"));
                if (cashPlanUtil.isJSONArray(items)) {
                    JSONArray dataArray = JSONArray.parseArray(items);
                    int counts = dataArray.size();
                    int count = 0;
                    for (int i = 0; i < dataArray.size(); i++) {
                        JSONObject dataJson = dataArray.getJSONObject(i);
                        String yxlsid = Util.null2String(dataJson.getString("yxlsid"));
                        writeLog(">>>>>postbackPaymentToWF>>>yxlsid=" + yxlsid);
                        if (cashPlanUtil.isKeyExistByWf(mainid, tablename, yxlsid)) {
                            message = "主键重复错误";
                        } else {
                            String sfgh = Util.null2String(dataJson.getString("sfgh"));
                            String ghje = Util.null2String(dataJson.getString("ghje"));
                            String sfje = Util.null2String(dataJson.getString("sfje"));
                            String newje = "1".equals(sfgh) ? ghje : sfje;
                            String field = "4734".equals(formId) ? "bcqkjehjxxyb" : "bcqkjehjxx";
                            cashPlanUtil.updateYbyktz(requestId, newje, field, formId, formtype);
                            dataJson.put("mainid", mainid);
                            boolean flag = cashPlanUtil.updateDetailData(tablename, dataJson);
                            if (flag) {
                                count++;
                            } else {
                                message = "参数字段类型错误";
                            }
                        }

                    }
                    data.put("sumcount", counts);
                    data.put("finishcount", count);
                    data.put("failcount", counts - count);
                } else {
                    data.put("error", "JSON数组格式化错误");
                    code = "PARAMETER_FORMAT_ERROR";
                    message = "参数格式转化异常";
                }
            }
        } catch (Exception e) {
            System.out.println("参数格式转化异常:" + e);
            data.put("error", "程序内部异常");
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        writeLog(">>>>>postbackPaymentToWF>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * @Description: 资金系统调用接口推送付款信息到建模台账
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/postbackPaymentToMD")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String postbackPaymentToMD(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String code = "SUCCESS";
        JSONObject data = new JSONObject();
        String message = "操作成功";
        CashPlanUtil cashPlanUtil = new CashPlanUtil();
        try {
            writeLog(">>>>>saveRateData>>>paramsJson=" + paramsJson);
            int modelId = Util.getIntValue(paramsJson.getString("modelId"), -1);
            if (!cashPlanUtil.isModelId(modelId)) {
                code = "PARAMETER_ERROR";
                data.put("error", "modelId不存在");
                message = "请求参数错误";
            } else {
                writeLog(">>>>>saveRateData>>>paramsJson=" + paramsJson);
                String items = Util.null2String(paramsJson.getString("items"));
                JSONArray dataArray = JSONArray.parseArray(items);
                if (cashPlanUtil.isJSONArray(items)) {
                    int counts = dataArray.size();
                    int count = 0;
                    for (int i = 0; i < dataArray.size(); i++) {
                        JSONObject dataJson = dataArray.getJSONObject(i);
                        String wyid = Util.null2String(dataJson.getString("wyid"));
                        writeLog(">>>>>saveRateData>>>wyid=" + wyid);
                        if (cashPlanUtil.isKeyExistByModel(modelId, wyid)) {
                            message = "主键重复错误";
                        } else {
                            int dataid = cashPlanUtil.insertModelData(modelId, dataJson);
                            writeLog(">>>>>saveRateData>>>dataid=" + dataid);
                            if (dataid > 0) {
                                count++;
                            } else {
                                message = "参数字段类型错误";
                            }
                        }
                    }
                    data.put("sumcount", counts);
                    data.put("finishcount", count);
                    data.put("failcount", counts - count);
                } else {
                    data.put("error", "JSON数组格式化错误");
                    code = "PARAMETER_FORMAT_ERROR";
                    message = "参数格式转化异常";
                }

            }
        } catch (Exception e) {
            System.out.println("参数格式转化异常:" + e);
            data.put("error", "程序内部异常");
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        writeLog(">>>>>doCreateRequest>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * @Description: 资金系统调用接口推送收款信息到建模台账
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023-09-10
     */
    @POST
    @Path("/postbackCollectionToMD")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String postbackCollectionToMD(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        String code = "SUCCESS";
        JSONObject data = new JSONObject();
        String message = "操作成功";
        CashPlanUtil cashPlanUtil = new CashPlanUtil();
        try {
            writeLog(">>>>>postbackCollectionToMD>>>paramsJson=" + paramsJson);
            int modelId = Util.getIntValue(paramsJson.getString("modelId"), -1);
            if (!cashPlanUtil.isModelId(modelId)) {
                code = "PARAMETER_ERROR";
                data.put("error", "modelId不存在");
                message = "请求参数错误";
            } else {
                writeLog(">>>>>postbackCollectionToMD>>>paramsJson=" + paramsJson);
                String items = Util.null2String(paramsJson.getString("items"));
                if (cashPlanUtil.isJSONArray(items)) {
                    JSONArray dataArray = JSONArray.parseArray(items);
                    int counts = dataArray.size();
                    int count = 0;
                    for (int i = 0; i < dataArray.size(); i++) {
                        JSONObject dataJson = dataArray.getJSONObject(i);
                        String wyid = Util.null2String(dataJson.getString("wyid"));
                        if (cashPlanUtil.isKeyExistByModel(modelId, wyid)) {
                            message = "主键重复错误";
                        } else {
                            String khmc = Util.null2String(dataJson.getString("khmc"));
                            khmc = khmc.replace("'", "''");
                            dataJson.put("khmc", khmc);
                            int dataid = cashPlanUtil.insertModelData(modelId, dataJson);
                            writeLog(">>>>>postbackCollectionToMD>>>dataid=" + dataid);
                            if (dataid > 0) {
                                count++;
                            } else {
                                message = "参数字段类型错误";
                            }
                        }

                    }
                    data.put("sumcount", counts);
                    data.put("finishcount", count);
                    data.put("failcount", counts - count);
                } else {
                    data.put("error", "JSON数组格式化错误");
                    code = "PARAMETER_FORMAT_ERROR";
                    message = "参数格式转化异常";
                }
            }
        } catch (Exception e) {
            System.out.println("参数格式转化异常:" + e);
            data.put("error", "程序内部异常");
            code = "SYSTEM_INNER_ERROR";
            message = "程序内部异常";
        }
        apidatas.put("code", code);
        apidatas.put("data", data);
        apidatas.put("message", message);
        writeLog(">>>>>postbackCollectionToMD>>>apidatas=" + apidatas.toString());
        return JSONObject.toJSONString(apidatas);
    }


}
